package com.blog.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.blog.dto.LoginDTO;
import com.blog.dto.RegisterDTO;
import com.blog.dto.Result;
import com.blog.entity.User;
import com.blog.service.UserService;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthGiteeRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/auth")
public class AuthController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> login(@RequestBody LoginDTO loginDTO) {
        User user = userService.login(loginDTO);

        if (user == null) {
            return ResponseEntity.badRequest().body(Map.of("message", "用户名或密码错误"));
        }

        // 登录成功，使用SaToken记录登录状态
        StpUtil.login(user.getId());

        // 返回用户信息和token
        Map<String, Object> result = new HashMap<>();
        result.put("tokenName", StpUtil.getTokenName());
        result.put("tokenValue", StpUtil.getTokenValue());
        result.put("user", user);

        return ResponseEntity.ok(result);
    }

    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> register(@RequestBody RegisterDTO registerDTO) {
        try {
            boolean success = userService.register(registerDTO);
            if (success) {
                return ResponseEntity.ok(Map.of("message", "注册成功"));
            } else {
                return ResponseEntity.badRequest().body(Map.of("message", "注册失败"));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("message", e.getMessage()));
        }
    }

    @PostMapping("/logout")
    public ResponseEntity<Map<String, Object>> logout() {
        StpUtil.logout();
        return ResponseEntity.ok(Map.of("message", "退出登录成功"));
    }

//    @RequestMapping("/{source}/render")
//    public void renderAuth(HttpServletResponse response,@PathVariable String source) throws IOException {
//
//
//        AuthRequest authRequest = getAuthRequest();
//        if (response == null) {
//            return;
//        }
//        if(source == null){
//            return;
//        }
//
//        response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
//    }
//
//    @RequestMapping("/callback")
//    public Result<Object> login(AuthCallback callback) {
//        AuthRequest authRequest = getAuthRequest();
//        AuthResponse<AuthUser> response = authRequest.login(callback);
//        if (response.ok()) {
//            AuthUser authUser = response.getData();
//            // 获取用户信息，例如昵称、头像等
//            String nickname = authUser.getNickname();
//            String avatar = authUser.getAvatar();
//            // 将用户信息保存到数据库或缓存中
//            userService.loginByGitee(authUser);
//
//            // ...
//            // 登录成功，使用SaToken记录登录状态
////            StpUtil.login(nickname);
//            // 返回用户信息和token
//            Map<String, Object> result = new HashMap<>();
//        }else{
//            return Result.fail("登录失败");
//        }
//        return Result.success(response);
//    }


}
